package org.jeecg.modules.demo.crm.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.demo.crm.entity.CProjectApplication;
import org.jeecg.modules.demo.crm.entity.CQuotation;
import org.jeecg.modules.demo.crm.entity.CQuotationMail;
import org.jeecg.modules.demo.crm.mapper.CLibraryMapper;
import org.jeecg.modules.demo.crm.mapper.CProjectApplicationMapper;
import org.jeecg.modules.demo.crm.mapper.CQuotationMailMapper;
import org.jeecg.modules.demo.crm.mapper.CQuotationMapper;
import org.jeecg.modules.demo.crm.service.ICQuotationService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 工程报价单
 * @Author: jeecg-boot
 * @Date:   2025-08-28
 * @Version: V1.0
 */
@Api(tags="工程报价单")
@RestController
@RequestMapping("/crm/cQuotation")
@Slf4j
public class CQuotationController extends JeecgController<CQuotation, ICQuotationService> {
	@Autowired
	private ICQuotationService cQuotationService;

	@Autowired
	private CQuotationMailMapper cQuotationMailMapper;

	@Autowired
	private CProjectApplicationMapper cProjectApplicationMapper;
	
	/**
	 * 分页列表查询
	 *
	 * @param cQuotation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "工程报价单-分页列表查询")
	@ApiOperation(value="工程报价单-分页列表查询", notes="工程报价单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CQuotation>> queryPageList(CQuotation cQuotation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Map<String, QueryRuleEnum> customeRuleMap = new HashMap<>();
		// 自定义多选的查询规则为：LIKE_WITH_OR
		customeRuleMap.put("name", QueryRuleEnum.LIKE_WITH_OR);
        QueryWrapper<CQuotation> queryWrapper = QueryGenerator.initQueryWrapper(cQuotation, req.getParameterMap(),customeRuleMap);
		Page<CQuotation> page = new Page<CQuotation>(pageNo, pageSize);
		IPage<CQuotation> pageList = cQuotationService.page(page, queryWrapper);
		pageList.getRecords().stream().forEach(record -> {
			Integer mailCount = cQuotationMailMapper.findByQuotationId(record.getId());
			//查询库
			CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(record.getProjectId());
			if(cProjectApplication != null){
				record.setLibraryId(cProjectApplication.getLibraryId());
			}

			record.setMailCount(mailCount);
		});

		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param cQuotation
	 * @return
	 */
	@AutoLog(value = "工程报价单-添加")
	@ApiOperation(value="工程报价单-添加", notes="工程报价单-添加")
	@RequiresPermissions("crm:c_quotation:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CQuotation cQuotation) {
		cQuotation.setCreateTime(new Date());
		cQuotationService.add(cQuotation);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param cQuotation
	 * @return
	 */
	@AutoLog(value = "工程报价单-编辑")
	@ApiOperation(value="工程报价单-编辑", notes="工程报价单-编辑")
	@RequiresPermissions("crm:c_quotation:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CQuotation cQuotation) {
		cQuotation.setUpdateTime(new Date());
		cQuotationService.edit(cQuotation);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "工程报价单-通过id删除")
	@ApiOperation(value="工程报价单-通过id删除", notes="工程报价单-通过id删除")
	@RequiresPermissions("crm:c_quotation:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		cQuotationService.delete(id);
		return Result.OK("删除成功!");
	}
	

	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "工程报价单-通过id查询")
	@ApiOperation(value="工程报价单-通过id查询", notes="工程报价单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CQuotation> queryById(@RequestParam(name="id",required=true) String id) {
		CQuotation cQuotation = cQuotationService.getById(id);
		if(cQuotation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(cQuotation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param cQuotation
    */
    @RequiresPermissions("crm:c_quotation:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CQuotation cQuotation) {
        return super.exportXls(request, cQuotation, CQuotation.class, "工程报价单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("crm:c_quotation:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CQuotation.class);
    }

	 /**
	  * 工程报价单-生成报价单
	  * @param id
	  * @return
	  */
	 @IgnoreAuth //免登陆 token
	 @ApiOperation(value="工程报价单-生成报价单", notes="工程报价单-生成报价单")
	 @GetMapping(value = "/generateQuotation")
	 public Result<CQuotationMail> generateQuotation(@RequestParam(name="id",required=true) String id) {
		 CQuotationMail data =  cQuotationService.generateQuotation(id);
		 return Result.OK(data);
	 }

}
